package com.oneday.ashop.console.resource;

import com.oneday.ashop.console.base.support.BaseServiceSupport;
import com.oneday.ashop.core.entity.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Controller;

import java.util.Date;
import java.util.List;

/**
 *  资源业务层
 * Created by admin on 2016/7/2.
 */
@Controller
public class ResourceService extends BaseServiceSupport{

    @Autowired
    private ResourceDao resourceDao;

    /**
     * 添加资源
     * @param resource
     */
    public void addResource(Resource resource) {
        resource.setCreateTime(new Date());
        resource.setAdminId(getCurrentAdmin().getId());
        resource.setAdminName(getCurrentAdmin().getName());
        resourceDao.addResource(resource);
    }

    /**
     * 删除资源
     * @param resourceId
     */
    public void deleteResource(Integer resourceId) {
        Update update = Update.update("status", Resource.STATUS_DELETE).set("lastModifyTime", new Date());
        resourceDao.findAndModifyResource(resourceId, update);
    }

    /**
     * 获得所有的资源
     * @return
     */
    public List<Resource> getALlResources() {
        return resourceDao.findAllResources();
    }
}
